﻿using Microsoft.Extensions.DependencyInjection;
using MyVideoPlayer.Models;
using MyVideoPlayer.Services.DI;
using MyVideoPlayer.ViewModels;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml;

namespace MyVideoPlayer.Views
{
    /// <summary>
    /// Interaction logic for MultiWinSet.xaml
    /// </summary>
    public partial class MultiWinSet : Window
    {
        /// <summary>
        /// 多窗口设置实例
        /// </summary>
        public static MultiWinSet? Instance { get; private set; }
        public readonly MultiWinSetViewModel _multiWinSetViewModel;
        /// <summary>
        /// 多窗口设置构造函数
        /// </summary>
        public MultiWinSet() : this(new MainWindowModel())
        {

        }
        /// <summary>
        /// 多窗口设置带参构造函数
        /// </summary>
        /// <param name="mainWindowModel"></param>
        public MultiWinSet(MainWindowModel mainWindowModel)
        {
            InitializeComponent();
            Instance = this;
            _multiWinSetViewModel = new MultiWinSetViewModel(mainWindowModel);
            this.DataContext = _multiWinSetViewModel;
        }
        /// <summary>
        /// 设置窗口加载时的事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_multiWinSetViewModel.WindowMap.Count > 0)
            {
                this.Hide();
                e.Cancel = true; // 阻止关闭
            }
            // 否则允许关闭，OnClosed会自动释放资源
            base.OnClosing(e);
        }
        /// <summary>
        /// 窗体关闭时释放资源
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            Instance = null; // 清理静态实例
            _multiWinSetViewModel.Dispose();
            base.OnClosed(e); // 调用基类方法
        }
    }
}